﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for QLNV
/// </summary>
public class QLNV
{
    ConnectionDB db = new ConnectionDB();
    NhanVien nv;
	public QLNV()
	{
	}

    public List<NhanVien> getAllNv()
    {
        List<NhanVien> list = new List<NhanVien>();
        ConnectionDB con = new ConnectionDB();
        string sql = "select * from nhanvien"; ;
        con.Cmd.CommandText = sql;
        SqlDataReader reader = con.Cmd.ExecuteReader();
        NhanVien nv = null;
        if (reader.HasRows)
        {
            while (reader.Read())
            {
                nv = new NhanVien();
                nv.Manv = reader.GetString(0);
                nv.Hotennv = reader.GetString(1);
                nv.Diachi = reader.GetString(2);
                nv.Namsinh = reader.GetDateTime(3);
                nv.Trinhdo = reader.GetString(4);
                nv.Socmnd = reader.GetDecimal(5);
                nv.Sodt = reader.GetDecimal(6);
                list.Add(nv);
            }
        }
        return list;
    }

    public int CheckAccount(string username, string password)
    {

        SqlParameter[] pa = new SqlParameter[2];
        pa[0] = new SqlParameter("@username", SqlDbType.NVarChar, 30);
        pa[0].Value = username;
        pa[1] = new SqlParameter("@password", SqlDbType.NVarChar, 30);
        pa[1].Value = password;
        return Convert.ToInt32(db.ExecScalar("SELECT COUNT(*) FROM nhanvien WHERE Ma_nv = @username AND Matkhau_nv = @password", pa));
    }

    public bool checkAccount(string userName, string password)
    {
        int check = CheckAccount(userName, password);
        if (check <= 0)
            return false;
        else
            return true;
    }

    public void insertNV()
    {
        nv = new NhanVien();
        ConnectionDB cn = new ConnectionDB();
        cn.Cmd.Parameters.Add(new SqlParameter("@ma", nv.Manv));
        cn.Cmd.Parameters.Add(new SqlParameter("@mk", nv.Matkhau));
        cn.Cmd.Parameters.Add(new SqlParameter("@ns", nv.Namsinh));
        cn.Cmd.Parameters.Add(new SqlParameter("@socmnd", nv.Socmnd));
        cn.Cmd.Parameters.Add(new SqlParameter("@trinhdo", nv.Trinhdo));
        cn.Cmd.Parameters.Add(new SqlParameter("@hoten", nv.Hotennv));
        cn.Cmd.Parameters.Add(new SqlParameter("@diachi", nv.Diachi));
        cn.Cmd.Parameters.Add(new SqlParameter("@sodt", nv.Sodt));
        cn.Cmd.Parameters.Add(new SqlParameter("@tt", nv.Tranghai));
        cn.Cmd.CommandType = CommandType.StoredProcedure;
        cn.Cmd.CommandText = "themnhanvien";
        cn.Cmd.ExecuteNonQuery();
    }
}